package com.mls.fivedistrict.server.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.dao.NewsMapper;
import com.mls.fivedistrict.dao.NewsNewsTagMapper;
import com.mls.fivedistrict.pojo.News;
import com.mls.fivedistrict.pojo.NewsLikeRecord;
import com.mls.fivedistrict.pojo.NewsNewsTag;
import com.mls.fivedistrict.server.NewsLikeRecordService;
import com.mls.fivedistrict.server.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 新闻 服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-05-15
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Autowired
    NewsMapper newsMapper;

    @Autowired
    NewsNewsTagMapper newsNewsTagMapper;

    @Autowired
    NewsLikeRecordService newsLikeRecordService;

    /**
     * 添加新闻实体
     * 添加新闻标签关系列表
     *
     * @param news
     * @return
     */
    @Override
    @Transactional
    public boolean insert(News news) {
        List<NewsNewsTag> newsNewsTagList = news.getNewsNewsTagList();
        Integer insert = newsMapper.insert(news);
        if (null != newsNewsTagList && newsNewsTagList.size() > 0) {
            for (NewsNewsTag newsNewsTag : newsNewsTagList) {
                newsNewsTag.setNewsId(news.getId());
            }
            newsNewsTagMapper.insertList(newsNewsTagList);
        }
        if (null != insert && insert >= 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改新闻实体
     * 修改新闻标签关系
     *
     * @param news
     * @return
     */
    @Override
    @Transactional
    public boolean updateById(News news) {
        Integer update = newsMapper.updateById(news);
        //修改前的标签关系列表
        List<NewsNewsTag> before = newsNewsTagMapper.selectByNewsId(news.getId());
        //修改后的标签关系列表
        List<NewsNewsTag> after = news.getNewsNewsTagList();
        //修改前有关系列表，删掉先
        if (null != before && before.size() > 0) {
            newsNewsTagMapper.deleteByNewsId(news.getId());
        }
        //修改后不为空，新增
        if (null != after && after.size() > 0) {
            for (NewsNewsTag newsNewsTag : after) {
                newsNewsTag.setNewsId(news.getId());
            }
            newsNewsTagMapper.insertList(after);
        }

        if (null != update && update >= 1) {
            return true;
        }
        return false;
    }

    @Transactional
    @Override
    public boolean addReadCount(Long id) {
        return newsMapper.addReadCount(id);
    }

    @Transactional
    @Override
    public boolean addLikeCount(Long id) {
        return newsMapper.addLikeCount(id);
    }

    @Transactional
    @Override
    public boolean addShareTimes(Long id) {
        return newsMapper.addShareTimes(id);
    }

    @Transactional
    @Override
    public boolean addCommentTimes(Long id) {
        return newsMapper.addCommentTimes(id);
    }

    @Transactional
    @Override
    public boolean cutReadCount(Long id) {
        return newsMapper.cutReadCount(id);
    }

    @Transactional
    @Override
    public boolean cutLikeCount(Long id) {
        return newsMapper.cutLikeCount(id);
    }

    @Transactional
    @Override
    public boolean cutShareTimes(Long id) {
        return newsMapper.cutShareTimes(id);
    }

    @Transactional
    @Override
    public boolean cutCommentTimes(Long id) {
        return newsMapper.cutCommentTimes(id);
    }

    @Override
    public News selectDetail(Long id) {
        News news = newsMapper.selectNewsDetail(id);
        if (RequestHolder.getCurrentUser() != null) {
            Long userId = RequestHolder.getCurrentUser().getId();
            Wrapper wrapper = Condition.create();
            wrapper.eq("news_id", id);
            wrapper.eq("user_id", userId);
            NewsLikeRecord newsLikeRecord = newsLikeRecordService.selectOne(wrapper);
            if (newsLikeRecord == null) {
                news.setLike(false);
            } else {
                news.setLike(true);
            }
        }

        return news;
    }

    @Override
    public int modifyReward() {
        int update = newsMapper.mofiyReward();
        return update;
    }

}
